<template>
	<view class="tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed customBack :bottomShadow="false" :backgroundColor="navBarBackgroundColor" id="navbar">
			<view slot="back" class='tn-custom-nav-bar__back' :style="[navBarStyle]">
				<text class='icon tn-icon-home-capsule-fill' @click="goHome"></text>
			</view>
		</tn-nav-bar>

		<swiper class="card-swiper" :circular="true" :autoplay="true" duration="500" interval="10000">
			<swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''">
				<view class="swiper-item image-banner">
					<image :src="item" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>

		<!-- 头部start-->
		<view class="shadow-function tn-margin-bottom-xl" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
			<view class="tn-flex tn-flex-direction-column tn-flex-row-center tn-flex-col-center">
				<view class="justify-content-item tn-flex-row-center">
					<view class="logo-pic tn-shadow">
						<view class="logo-image">
							<view class="tn-shadow-blur" style=""
								:style="[getBackgroundStyle(groupOrder.groupLeaderHeadSculpture)]">
							</view>
						</view>
					</view>
				</view>
				<view class="justify-content-item">
					<view class="tn-flex tn-flex-col-center">
						<view class="tn-color-white">
							<view class="tn-text-center tn-text-lg tn-text-bold tn-margin-sm">
								团长：{{groupOrder.groupLeader}}
							</view>
							<view class="tn-text-ellipsis tn-text-sm">
								<text class="">团购号：{{groupOrder.groupId}}</text>
							</view>
						</view>
					</view>
				</view>

			</view>
		</view>
		<!-- 头部 end-->

		<view class="group-wrap tn-bg-white" id="page_tips">
			<view class="tn-padding">
				<view class="tn-flex tn-flex-row-between tn-flex-col-center">
					<view class="justify-content-item tn-text-bold tn-text-xl">
						参团成员
					</view>
					<view class="justify-content-item tn-text">
						<block v-if="groupOrder.groupDaysRemaining > 0">
							拼团倒计时：
							<tn-count-down separator="ch" ref="countDown" :timestamp="groupOrder.groupDaysRemaining"
								fontSize="28" separatorSize="28" :autoplay="false" @end="endWatch"></tn-count-down>
						</block>
						<block v-else>
							<text :class="[`tn-color-${groupInfo.color}`]">{{groupInfo.text}}</text>
						</block>
					</view>
				</view>
			</view>

			<view class="tn-margin">
				<tn-avatar-group :lists="groupOrder.headSculptureDTOS"></tn-avatar-group>
			</view>

			<!-- 边距间隔 -->
			<view class="tn-strip-bottom"></view>

			<view class="tn-margin">
				<view class="tn-flex tn-flex-row-between tn-flex-col-center">
					<view class="justify-content-item tn-text-bold tn-text-xl">
						请选择参与团购的订单
					</view>
				</view>
			</view>

			<lm-order-list v-if="isLoaded&&groupOrder.projectList.length>0" :orderList="groupOrder.projectList"
				:oepnSwitch="true" @checkedClick="checkedClick"></lm-order-list>
			<view v-else>
				<tn-empty mode="order"></tn-empty>
			</view>
		</view>
		<!-- 团购按钮-->
		<view class="footerfixed dd-glass tn-padding-left-sm tn-padding-right tn-padding-top-xs tn-padding-bottom-sm">
			<tn-goods-nav :options="countOptions" :buttonGroups="customButtonGroups" buttonType="round"
				:safeAreaInsetBottom="true" @optionClick="onOptionClick" @buttonClick="onButtonClick"></tn-goods-nav>
		</view>

		<view class='tn-tabbar-height'></view>
	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js';
	import list from '@/libs/components/lm-order-list/lm-order-list.vue';
	import {
		queryGroupBuyDetailByOrderId
	} from "@/api";
  import {
    addGroup, exitGroupApi
  } from "@/api/groupBuy";
	import {
		nextTick
	} from "vue";
  import {set} from "@vue/composition-api";
	export default {
		name: 'TemplateMe',
		mixins: [template_page_mixin],
		components: {
			'lm-order-list': list
		},
		data() {
			return {
				isLoaded: false,
				cardCur: 0,
				groupStatus: global.staticDict.groupStatus,
				swiperList: global.imgUrl.bgImgUrlList,
        orderId: '',
				projectList: [],
				groupOrder: {},
				checkedOrderId: null,
				groupInfo: {
					text: "",
					color: "",
				},
				navBarStyle: {
					color: '#FFFFFF',
					opacity: 1,
					display: 'flex'
				},
				navBarBackgroundColor: 'rgba(255, 255, 255, 0)',
				timestamp: 18,
				countOptions: [{
					icon: 'service-simple-fill',
					openType: "contact",
					text: '在线客服',
				}],
				customButtonGroups: [{
					text: '加入团购',
					backgroundColor: 'tn-cool-bg-color-5--reverse',
					color: '#FFFFFF'
				}]
			}
		},
    //页面再次显示有groupId的时候自动调接口刷新
    onShow() {
      if(this.groupId) {
        this.getGroupBuyDetail(this.groupId)
      }
    },
		async onLoad(params) {
			await this.$onLaunched
			this.groupId = params.groupId
      this.orderId = params.orderId;
      if (params.groupStatus === "2") {
        this.groupStatus = params.groupStatus;
        this.customButtonGroups[0].text = '退出团购'
      }
			await this.getGroupBuyDetail(params.groupId);
			this.isLoaded = true
		},

		methods: {
      // 退出团购
      async exitGroup() {
        try {
          const res = await exitGroupApi(this.orderId);
          if(res.code === 200) {
            uni.showToast({
              title: '退出团购成功',
              icon: 'none'
            })
            setTimeout(()=>{
              uni.navigateBack();
            })
          } else {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        } catch (e) {
          console.log(e)
        }
      },
			async acceptOrder() {
				try {
					if (!this.checkedOrderId) {
						uni.showToast({
							title: '请选择团购订单',
							icon: 'error'
						})
						return;
					}
					const res = await addGroup({
						groupId: this.groupOrder.groupId,
						orderId: this.checkedOrderId,
					})
					if (res.code === 200) {
						this.isLoaded = false
						uni.showToast({
							title: '加入团购成功',
							icon: 'success'
						})
						this.checkedOrderId = null
						await this.getGroupBuyDetail(this.groupId)
						this.isLoaded = true
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				} catch (e) {
					console.log(e)
				}
			},
			async getGroupBuyDetail(id) {
				try {
					const res = await queryGroupBuyDetailByOrderId(id)
					if (res.code === 200) {
						this.groupOrder = res.data
						console.log(this.groupOrder.whetherEmptyGroup)
						if (this.groupOrder.whetherEmptyGroup == 1) {
							uni.reLaunch({
								url: '/homepages/groupMiss'
							})
						}
						this.groupOrder.projectList.forEach(item => {
							if (item.technicalSelection == '') {
								item.techList = []
							} else {
								item.techList = item.technicalSelection.split(',')
							}
							if (item.includingServices == '') {
								item.servicesList = []
							} else {
								item.servicesList = item.includingServices.split(',')

							}
						})
					}
					this.getGroupInfo()
				} catch (e) {
					console.log(e)
				}
			},
			//获取头像
			getBackgroundStyle(imageUrl) {
				return {
					'background-image': `url(${imageUrl})`,
					'width': '110rpx;height: 110rpx',
					'background-size': 'cover'
				}
			},
			//刷新团购信息
			getGroupInfo() {
				//进行中
				if (this.groupOrder.groupDaysRemaining > 0) {
					this.$nextTick(function() {
						this.$refs.countDown.start();
					})
				} else if (this.groupOrder.groupStatus == this.groupStatus.groupSuccess) {
					this.groupInfo.color = 'blue';
					this.groupInfo.text = '拼团成功';
					this.customButtonGroups[0].disable = true;
				} else if (this.detailInfo.groupStatus == this.groupStatus.groupFail) {
					this.groupInfo.color = 'purplered';
					this.groupInfo.text = '拼团未成功';
					this.customButtonGroups[0].disable = true;
				} else if (this.detailInfo.groupStatus == this.groupStatus.groupingGoing) {
					this.customButtonGroups[0].disable = false;
				} else {
					this.groupInfo.color = 'gray--dark';
					this.groupInfo.text = '拼团已结束';
					this.customButtonGroups[0].disable = true;
				}

			},
			//选中
			checkedClick(param) {
				this.checkedOrderId = param.orderId
			},
			onOptionClick(param) {
				console.log(param.index)
			},
			onButtonClick(param) {
				console.log(param.index)
				if (param.index == 0) {
          // 退团
          if (this.groupStatus === "2") {
            this.exitGroup();
            return;
          }
					this.acceptOrder()
				}
			},
			endWatch() {
				this.getGroupBuyDetail(this.groupId)
			},
		}
	}
</script>

<style lang="scss" scoped>
	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 50%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.03);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		// color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.05;
			background-color: #000000;
		}
	}

	/* 间隔线 start*/
	.tn-strip-bottom {
		width: 100%;
		border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
	}


	/* 轮播视觉差 start */
	.card-swiper {
		height: 470rpx !important;
	}


	.card-swiper swiper-item {
		width: 750rpx !important;
		left: 0rpx;
		box-sizing: border-box;
		// padding: 0rpx 30rpx 90rpx 30rpx;
		overflow: initial;
	}

	.card-swiper swiper-item .swiper-item {
		width: 100%;
		display: block;
		height: 100%;
		transform: scale(1);
		transition: all 0.2s ease-in 0s;
		will-change: transform;
		overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item {
		transform: none;
		transition: all 0.2s ease-in 0s;
		will-change: transform;
	}

	.image-banner {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-banner image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示点 start*/
	.indication {
		z-index: 9999;
		width: 100%;
		height: 36rpx;
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		opacity: 0;
	}

	.spot {
		background-color: #FFFFFF;
		opacity: 0.6;
		width: 10rpx;
		height: 10rpx;
		border-radius: 20rpx;
		top: -60rpx;
		margin: 0 8rpx !important;
		position: relative;
	}

	.spot.active {
		opacity: 1;
		width: 30rpx;
		background-color: #FFFFFF;
	}

	/* 顶部阴影 */
	.shadow-function {
		position: relative;
		z-index: 1;
		margin-top: -450rpx;
		padding-bottom: 110rpx;
		background-image: linear-gradient(rgba(255, 255, 255, 0.01), rgba(0, 0, 0, 0.4));
	}

	/* 用户头像 start */
	.logo-image {
		width: 110rpx;
		height: 110rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border: 6rpx solid rgba(255, 255, 255, 0.25);
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 50%;
		overflow: hidden;
		// background-color: #FFFFFF;
	}

	/* 内容*/
	.group-wrap {
		position: relative;
		z-index: 1;
		// padding: 20rpx 30rpx;
		margin-top: -130rpx;
		margin-bottom: 40rpx;
		border-radius: 30rpx 30rpx 0 0;
	}

	/* 阴影 start*/
	.mould-shadow {
		border-radius: 15rpx;
		box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
	}

	.tn-tabbar-height {
		min-height: 120rpx;
		height: calc(140rpx + env(safe-area-inset-bottom) / 2);
	}

	/* 图标容器5 start */
	.icon5 {
		&__item {
			// width: 30%;
			background-color: #FFFFFF;
			border-radius: 10rpx;
			padding: 0rpx;
			margin: 0rpx;
			transform: scale(1);
			transition: transform 0.3s linear;
			transform-origin: center center;

			&--icon {
				width: 96rpx;
				height: 96rpx;
				border-radius: 50%;
				margin-bottom: 18rpx;
				position: relative;
				z-index: 1;
			}
		}
	}

	/* 底部tabbar start*/
	.footerfixed {
		position: fixed;
		width: 100%;
		bottom: 0;
		z-index: 999;
		background-color: #FFFFFF;
		box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
	}

	/* 毛玻璃*/
	.dd-glass {
		width: 100%;
		backdrop-filter: blur(20rpx);
		-webkit-backdrop-filter: blur(20rpx);
	}
</style>